home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / oleo-1_4.lha / oleo-1.4 / font.c < prev    next >
C/C++ Source or Header  |  1993-05-21  |  7KB  |  347 lines

  1. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.  
  18. #include <ctype.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include "font.h"
  22. #include "window.h"
  23. #include "io-abstract.h"
  24. #include "cmd.h"
  25. #include "io-x11.h"
  26. #include "io-term.h"
  27. #include "io-utils.h"
  28.  
  29.  
  30.  
  31.  
  32. static char * default_oleo_name = "default";
  33. static char * default_x_name = "*times-medium-r-*";
  34. static char * default_ps_name = "Times-Roman";
  35.  
  36. static struct font_names * font_names = 0;
  37. struct font_memo * font_list = 0;
  38. struct font_memo * the_default_font = 0;
  39.  
  40.  
  41. #ifdef __STDC__
  42. void
  43. define_font (char * oleo_name, char * x_name, char * ps_name)
  44. #else
  45. void
  46. define_font (oleo_name, x_name, ps_name)
  47.      char * oleo_name;
  48.      char * x_name;
  49.      char * ps_name;
  50. #endif
  51. {
  52.   struct font_names * fn;
  53.  
  54.   for (fn = font_names; fn; fn = fn->next)
  55.     if (!stricmp(fn->oleo_name, oleo_name))
  56.       {
  57.     if (fn->x_name)
  58.       free (fn->x_name);
  59.     if (fn->ps_name)
  60.       free (fn->ps_name);
  61.     break;
  62.       }
  63.   if (!fn)
  64.     {
  65.       fn = (struct font_names *)ck_malloc (sizeof (*fn));
  66.       fn->oleo_name =  strdup (oleo_name);
  67.       fn->next = font_names;
  68.       font_names = fn;
  69.     }
  70.   fn->x_name = strdup (x_name);
  71.   fn->ps_name = strdup (ps_name);
  72. }
  73.  
  74.  
  75. #ifdef __STDC__
  76. static struct font_names * 
  77. find_font_name (char * name)
  78. #else
  79. static struct font_names * 
  80. find_font_name (name)
  81.      char * name;
  82. #endif
  83. {
  84.   struct font_names * fn = font_names;
  85.   if (!name || says_default (name))
  86.     name = "default";
  87.  
  88.   while (fn)
  89.     {
  90.       if (!stricmp (name, fn->oleo_name))
  91.     return fn;
  92.       fn = fn->next;
  93.     }
  94.   io_error_msg ("Unknown font family %s\n", name);
  95.   return 0;            /* never reached, actually */
  96. }
  97.  
  98.  
  99. /* For backwards compatability, there are created-on-demand fonts
  100.  * who's oleo name is a concatenation of their x and postscript names.
  101.  */
  102.  
  103. #ifdef __STDC__
  104. static struct font_names *
  105. matching_font_names (char * x_name, char * ps_name)
  106. #else
  107. static struct font_names *
  108. matching_font_names (x_name, ps_name)
  109.      char * x_name;
  110.      char * ps_name;
  111. #endif
  112. {
  113.   struct font_names * fn;
  114.   for (fn = font_names; fn; fn = fn->next)
  115.     {
  116.       if (   stricmp (fn->oleo_name, "default")
  117.       && !stricmp (ps_name, fn->ps_name)
  118.       && !stricmp (x_name, fn->x_name))
  119.     return fn;
  120.     }
  121.   {
  122.     char * oleo_name = mk_sprintf ("%s,%s", x_name, ps_name);
  123.     define_font (oleo_name, x_name, ps_name);
  124.     fn = find_font_name (oleo_name);
  125.     free (oleo_name);
  126.     return fn;
  127.   }
  128. }
  129.  
  130.  
  131.  
  132. #ifdef __STDC__
  133. struct font_memo *
  134. intern_font (char * oleo_name, double scale)
  135. #else
  136. struct font_memo *
  137. intern_font (oleo_name, scale)
  138.      char * oleo_name;
  139.      double scale;
  140. #endif
  141. {
  142.   struct font_names *names = find_font_name (oleo_name);
  143.   struct font_memo *it = font_list;
  144.   while (it)
  145.     {
  146.       if ((scale == font_list->scale) && (names == font_list->names))
  147.     return it;
  148.       it = it->next;
  149.     }
  150.   it = (struct font_memo *)ck_malloc (sizeof (*it));
  151.   it->scale = scale;
  152.   it->names = names;
  153.   it->next = font_list;
  154.   font_list = it;
  155.   return it;
  156. }
  157.  
  158. #ifdef __STDC__
  159. struct font_memo * 
  160. default_font (void)
  161. #else
  162. struct font_memo * 
  163. default_font ()
  164. #endif
  165. {
  166.   return the_default_font;
  167. }
  168.  
  169. #ifdef __STDC__
  170. struct font_memo *
  171. matching_font (char * x_name, char * ps_name, double scale)
  172. #else
  173. struct font_memo *
  174. matching_font (x_name, ps_name, scale)
  175.      char * x_name;
  176.      char * ps_name;
  177.      double scale;
  178. #endif
  179. {
  180.   struct font_names * fn = matching_font_names (x_name, ps_name);
  181.   return intern_font (fn->oleo_name, scale);
  182. }
  183.  
  184. /* This is for the benefit of oleofile.c only */
  185.  
  186. #ifdef __STDC__
  187. struct font_memo *
  188. parsed_matching_font (char * fullname)
  189. #else
  190. struct font_memo *
  191. parsed_matching_font (fullname)
  192.      char * fullname;
  193. #endif
  194. {
  195.   char x_name[1000];
  196.   char ps_name[1000];
  197.   char *p;
  198.   char *p2;
  199.   double scale;
  200.  
  201.   for (p = fullname; isspace (*p); ++p);
  202.   for (p2 = x_name; *p && (*p != ',') && !isspace (*p); *p2++ = *p++);
  203.   *p2 = '\0';
  204.   if (x_name[0] == '\0' || says_default(x_name))
  205.     strcpy (x_name, default_x_name);
  206. /* Got rid of the weird defined font-name expansion here. 
  207.  * These auto-magical fonts are only for the sake of reading old files
  208.  * which don't use that (cough) feature.
  209.  */
  210.   while (isspace (*p))   ++p;
  211.   if (*p == ',')    ++p;
  212.   while (isspace (*p))  ++p;
  213.   for (p2 = ps_name; *p && (*p != ',') && !isspace (*p); *p2++ = *p++) ;
  214.   *p2 = '\0';
  215.  
  216.   if (ps_name[0] == '\0' || says_default(x_name))
  217.     strcpy (ps_name, default_ps_name);
  218.   while (isspace (*p))     ++p;
  219.   if (*p == ',')     ++p;
  220.   while (isspace (*p))     ++p;
  221.  
  222.   if (isdigit (*p))
  223.     {
  224.       errno = 0;
  225.       scale = atof (p);
  226.       if (errno)
  227.     scale = 1.;
  228.     }
  229.   else
  230.     scale = 1.;
  231.  
  232.   return matching_font (x_name, ps_name, scale);
  233. }
  234.  
  235.  
  236.  
  237. #ifdef __STDC__
  238. void 
  239. set_region_font (struct rng *rng, char * oleo_name, double scale)
  240. #else
  241. void 
  242. set_region_font (rng, oleo_name, scale)
  243.      struct rng *rng;
  244.      char * oleo_name;
  245.      double scale;
  246. #endif
  247. {
  248.   struct font_memo *font = ((oleo_name || scale != 1.0)
  249.                 ? intern_font (oleo_name, scale)
  250.                 : 0);
  251.   CELL * cp;
  252.   
  253.   make_cells_in_range (rng);
  254.   cp = next_cell_in_range ();
  255.   while (cp)
  256.     {
  257.       cp->cell_font = font;
  258.       cp = next_cell_in_range ();
  259.     }
  260.   io_redo_region (rng);
  261. }
  262.  
  263. #ifdef __STDC__
  264. void 
  265. flush_fonts (void)
  266. #else
  267. void 
  268. flush_fonts ()
  269. #endif
  270. {
  271.   CELL * cp;
  272.   struct rng rng;
  273.   rng.lr = MIN_ROW;
  274.   rng.hr = MAX_ROW;
  275.   rng.lc = MIN_COL;
  276.   rng.hc = MAX_COL;
  277.   find_cells_in_range (&rng);
  278.   cp = next_cell_in_range ();
  279.   while (cp)
  280.     {
  281.       cp->cell_font = 0;
  282.       cp = next_cell_in_range ();
  283.     }
  284. }
  285.  
  286. #ifdef __STDC__
  287. void
  288. set_x_default_font (char * str)
  289. #else
  290. void
  291. set_x_default_font (str)
  292.      char * str;
  293. #endif
  294. {
  295.   define_font (default_oleo_name, str, the_default_font->names->ps_name);
  296.   io_repaint ();
  297. }
  298.  
  299. #ifdef __STDC__
  300. void
  301. set_ps_font_cmd (char * ps_name)
  302. #else
  303. void
  304. set_ps_font_cmd (ps_name)
  305.      char * ps_name;
  306. #endif
  307. {
  308.   define_font (default_oleo_name, the_default_font->names->x_name, ps_name);
  309. }
  310.  
  311. #ifdef __STDC__
  312. void
  313. set_default_font (char * name, double scale)
  314. #else
  315. void
  316. set_default_font (name, scale)
  317.      char * name;
  318.      double scale;
  319. #endif
  320. {
  321.   struct font_names * fn = find_font_name (name);
  322.   if (fn != find_font_name (default_oleo_name))
  323.     define_font (default_oleo_name, fn->x_name, fn->ps_name);
  324.   the_default_font = intern_font (default_oleo_name, scale);
  325. #ifdef HAVE_X11_X_H
  326.   if (using_x)
  327.     set_x_default_point_size (cell_font_point_size);
  328.   else
  329. #endif
  330.     io_repaint ();
  331. }
  332.  
  333.  
  334. #ifdef __STDC__
  335. void 
  336. init_fonts (void)
  337. #else
  338. void 
  339. init_fonts ()
  340. #endif
  341. {
  342.   define_font (default_oleo_name, default_x_name, default_ps_name);
  343.   define_font ("times", default_x_name, default_ps_name);
  344.   define_font ("f9x15", "9x15", "sdlkfj");
  345.   the_default_font = intern_font (default_oleo_name, 1.0);
  346. }
  347.